﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;

namespace PracaInz_v0
{
    class ExternalVectors
    {
        public void ClassifyNewVect_SingleTreeCMD(DataTable tree, DataTable vect, string mode, string name = "ClassifiedVectors.txt")
        {
            DataTable vectMean = vect.Copy();
            vectMean.Columns.Add("Mean", typeof(double));

            List<Task> LT = new List<Task>();

            if (mode == "n")
            {
                for (int i = 0; i < vectMean.Rows.Count; i++)
                {
                    int tmpi = i;
                    LT.Add(Task.Run(() =>
                        {
                            vectMean.Rows[tmpi]["Mean"] = findMean(tree, vectMean.Rows[tmpi], 0);
                        }));
                }
            }
            else if (mode == "r")
            {
                for (int i = 0; i < vectMean.Rows.Count; i++)
                {
                    int tmpi = i;
                    LT.Add(Task.Run(() =>
                        {
                            vectMean.Rows[tmpi]["Mean"] = findMeanRCMD(tree, vectMean.Rows[tmpi], 0);
                        }));
                }
            }
            Task.WaitAll(LT.ToArray());
            LoadData.save(vectMean, name, App.ARGS[3]);
        }

        public void ClassifyNewVect_SingleTree(DrawTree dtree, DataTable vect, bool mode, string name = "ClassifiedVectors.txt")
        {
            DataTable vectMean = vect.Copy();
            vectMean.Columns.Add("Mean", typeof(double));

            List<Task> LT = new List<Task>();

            if (!mode)  // normal
            {
                //int i = 0;
                Parallel.For(0, vectMean.Rows.Count, (i) =>
                {
                    vectMean.Rows[i]["Mean"] = findMean(dtree.TreeData, vectMean.Rows[i], 0);
                });
                //for (int i = 0; i < vectMean.Rows.Count; i++)
                //{
                //    int tmpi = i;
                //    LT.Add(Task.Run(() =>
                //    {
                //        vectMean.Rows[tmpi]["Mean"] = findMean(dtree.TreeData, vectMean.Rows[tmpi], 0);
                //    }));
                //}
            }
            else if (mode) //regresion
            {
                Parallel.For(0, vectMean.Rows.Count, (i) =>
                {
                    vectMean.Rows[i]["Mean"] = findMeanR(dtree, vectMean.Rows[i], 0);
                });
                //for (int i = 0; i < vectMean.Rows.Count; i++)
                //{
                //    int tmpi = i;
                //    LT.Add(Task.Run(() =>
                //    {
                //        vectMean.Rows[tmpi]["Mean"] = findMeanR(dtree, vectMean.Rows[tmpi], 0);
                //    }));
                //}
            }
            Task.WaitAll(LT.ToArray());
            LoadData.save(vectMean, name, "\t");
        }

        public void ClassifyNewVect_Forest(CompleatTree[] forest, DataTable vect, bool mode, string name = "ClassifiedVectors.txt")
        {
            DataTable vectMean = vect.Copy();
            vectMean.Columns.Add("Mean", typeof(double));

            if (!mode)  // normal
            {
                Parallel.For(0, vectMean.Rows.Count, (i) =>
                {
                    int x = i;
                    double[] means = new double[forest.Count()];

                    for (int j = 0; j < forest.Count(); j++)
			        {
                        means[j] = findMean(forest[j].TreeView.TreeData, vectMean.Rows[x], 0);
			        }

                    lock (vectMean)
                    {
                        vectMean.Rows[x]["Mean"] = Math.Round(means.Average(), 5);    
                    }
                    
                });               
            }
            else if (mode) //regresion
            { }

            LoadData.save(vectMean, name, "\t");
        }

        double findMeanR(DrawTree DTree, DataRow row, int treeRow)
        {
            int attr = int.Parse(DTree.TreeData.Rows[treeRow][9].ToString());
            double value = double.Parse(DTree.TreeData.Rows[treeRow][10].ToString());
            int LeftChild = int.Parse(DTree.TreeData.Rows[treeRow][4].ToString());
            int RightChild = int.Parse(DTree.TreeData.Rows[treeRow][5].ToString());

            double mean = 0;
            if (attr != 0)
            {
                if (double.Parse(row[attr - 1].ToString()) >= value)  //prawa strona
                {
                    if (RightChild != 0)
                    {
                        mean = findMeanR(DTree, row, RightChild - 1);
                    }
                    else
                    {
                        mean = DTree.p[treeRow].cl.NewMean;
                    }
                }
                else                                            //lewa strona
                {
                    if (LeftChild != 0)
                    {
                        mean = findMeanR(DTree, row, LeftChild - 1);
                    }
                    else
                    {
                        mean = DTree.p[treeRow].cl.NewMean;
                    }
                }
            }
            else
                mean = DTree.p[treeRow].cl.NewMean;

            return mean;
        }

        double findMean(DataTable Tree, DataRow row, int treeRow)
        {            
            int attr = int.Parse(Tree.Rows[treeRow][9].ToString());
            double value = double.Parse(Tree.Rows[treeRow][10].ToString());
            int LeftChild = int.Parse(Tree.Rows[treeRow][4].ToString());
            int RightChild = int.Parse(Tree.Rows[treeRow][5].ToString());

            double mean = 0;
            if (attr != 0)
            {
                if (double.Parse(row[attr - 1].ToString()) >= value)  //prawa strona
                {
                    if (RightChild != 0)
                    {
                        mean = findMean(Tree, row, RightChild - 1);
                    }
                    else
                        mean = double.Parse(Tree.Rows[treeRow][6].ToString());
                }
                else                                            //lewa strona
                {
                    if (LeftChild != 0)
                    {
                        mean = findMean(Tree, row, LeftChild - 1);
                    }
                    else
                        mean = double.Parse(Tree.Rows[treeRow][6].ToString());
                }
            }
            else
                mean = double.Parse(Tree.Rows[treeRow][6].ToString());

            return mean;
        }

        double findMeanRCMD(DataTable Tree, DataRow row, int treeRow)
        {
            int attr = int.Parse(Tree.Rows[treeRow][9].ToString());
            double value = double.Parse(Tree.Rows[treeRow][10].ToString());
            int LeftChild = int.Parse(Tree.Rows[treeRow][4].ToString());
            int RightChild = int.Parse(Tree.Rows[treeRow][5].ToString());

            double mean = 0;
            if (attr != 0)
            {
                if (double.Parse(row[attr - 1].ToString()) >= value)  //prawa strona
                {
                    if (RightChild != 0)
                    {
                        mean = findMeanRCMD(Tree, row, RightChild - 1);
                    }
                    else
                        mean = double.Parse(Tree.Rows[treeRow][12].ToString());
                }
                else                                            //lewa strona
                {
                    if (LeftChild != 0)
                    {
                        mean = findMeanRCMD(Tree, row, LeftChild - 1);
                    }
                    else
                        mean = double.Parse(Tree.Rows[treeRow][12].ToString());
                }
            }
            else
                mean = double.Parse(Tree.Rows[treeRow][12].ToString());

            return mean;
        }
    }
}
